define(function(require, exports, module) {



    var google = function() {
        this.point = null;
        this.marker = null;
        this.infowindow = null;
        this.map = null

    }
    google.prototype.load = function() {
        if (typeof(google) == 'undefined') {
            var script = document.createElement("script");
            var keys = ["AIzaSyAOne2X4zK1sQPbsiSK6oSaF8U7BJkPmt0", "AIzaSyBSvfDvgbHw88c3HOG2lWP5A4bDnu6oPVI", "AIzaSyDpx3CSeFhC9V0FZ8A5BLIIsfOpiRpuDhM"];
            script.src = "http://maps.googleapis.com/maps/api/js?key={0}&sensor=false&callback=wm.map.maps.init".format(keys[Math.floor(Math.random() * keys.length + 1) - 1]);
            document.body.appendChild(script);
        } else {
            this.init();
        }

    };
    google.prototype.init = function() {
        var _self = this;
        _self.point = new google.maps.LatLng(wm.map.op.lat, wm.map.op.lng);
        var myOptions = {
            zoom: 15, // 缩放级别
            center: _self.point,
            mapTypeId: google.maps.MapTypeId.ROADMAP, // 显示普通的街道地图
            mapTypeControl: false, // 地图类型控件
            overviewMapControl: true, // 总览图控件
            scaleControl: false, // 比例控件
            streetViewControl: false, // 街景视图
            panControl: true //平移控件

        };

        _self.map = new google.maps.Map(document.getElementById("l-map"), myOptions);

        _self.marker = new google.maps.Marker({
            position: _self.point,
            map: _self.map
        });
        _self.marker.setDraggable(true); // 设置可拖拽
        _self.map.addListener("drag", function() {
            var cp = _self.map.center;
            _self.marker.setPosition(cp);
        });
        _self.map.addListener("dragend", function() {
            _self.infowindow.close();
            _self.showAddress(_self.marker);
        });
        //初始化信息窗口
        _self.infowindow = new google.maps.InfoWindow();

        google.maps.event.addListener(_self.marker, 'dragend', function() {
            _self.infowindow.close();
            _self.showAddress(_self.marker);
        });
        google.maps.event.addListener(_self.marker, 'click', function() {
            _self.infowindow.open(_self.map, _self.marker);
            // _self.showAddress(_self.marker);
        });
        _self.infowindow = new google.maps.InfoWindow({
            content: "<div style='width:180px'>原本位置 {0} ,移动红点修改位置!你也可以直接修改上方位置系统自动定位!</div>".format(wm.map.op.adr)
        });

        if (!wm.map.is_change) {
            _self.infowindow.open(_self.map, _self.marker);
        } else {
            _self.search();
        }

        if (wm.map.is_default) {
            _self.locate();
        }
    };


    google.prototype.locate = function() {
        var _self = this;
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var al = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                _self.marker.setPosition(al);
                _self.map.panTo(al);
                _self.infowindow.setContent("<div style='width:200px'>定位成功这是你当前的位置!,移动红点标注目标位置，你也可以直接修改上方位置,系统自动定位!</div>");
                _self.infowindow.open(_self.map, _self.marker);
                _self.showAddress(_self.marker, true);
            });
        }
    };
    google.prototype.search = function() {
        var self = this;
        var geocoder = new google.maps.Geocoder();
        var addr = wm.map.el.suggestId.val();
        addr = addr.length > 0 ? addr : wm.map.op.adr;
        geocoder.geocode({
            address: addr
        }, function geoResults(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var al = results[0].geometry.location;
                self.infowindow.setContent("<div style='width:200px'>搜索位置 " + results[0].formatted_address + " ,移动红点修改位置!你也可以直接修改上方位置系统自动定位!</div>")
                self.infowindow.open(self.map, self.marker);
                self.marker.setPosition(al);
                self.map.panTo(al);
                self.showAddress(self.marker, true);
            }
        });
    };
    google.prototype.showAddress = function(marker, callout) {
        var latlng = marker.getPosition();
        var geocoder = new google.maps.Geocoder();
        var self = this;
        //根据经纬度获取地址信息
        geocoder.geocode({
            'latLng': latlng
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                    var formatted_address = results[0].formatted_address;
                    var addr_str_length = formatted_address.indexOf("邮政编码");
                    if (addr_str_length > 0) {
                        formatted_address = (formatted_address.substring(0, addr_str_length));
                    }
                    if (!callout) {
                        self.infowindow.setContent("<div style='width:190px'>标注位置 " + formatted_address + " ,移动红点修改位置!你也可以直接修改上方位置系统自动定位!</div>");
                    };
                    var el = wm.map.el;
                    el.lat.val(latlng.lat());
                    el.lng.val(latlng.lng());
                    el.suggestId.val(formatted_address);
                }
            } else {
                console.log("Geocoder failed due to: " + status);
            }
        });
    };

    return google;
});
